//
//  YJ_StatusCell.m
//  YouJoin
//
//  Created by MacBookPro on 15/11/10.
//  Copyright © 2015年 MacBookPro. All rights reserved.
//

// 昵称的字体
#define YJ_NameFont [UIFont systemFontOfSize:10]
// 正文的字体
#define YJ_TextFont [UIFont systemFontOfSize:12]

#import "YJ_StatusCell.h"
#import "YJ_Status.h"
#import "YJ_StatusFrame.h"

@interface YJ_StatusCell()
/**
 *  头像
 */
@property (nonatomic, weak) UIImageView *iconPicView;
/**
 *  昵称
 */
@property (nonatomic, weak) UILabel *usernameView;
/**
 *  时间
 */
@property (nonatomic, weak) UILabel *timerView;
/**
 *  正文
 */
@property (nonatomic, weak) UILabel *textContentView;
/**
 *  配图
 */
@property (nonatomic, weak) UIImageView *pictureView;

@property (nonatomic, weak) UIView *cellPaddingView;

@property (nonatomic, weak) UIView *commentView;

@end


@implementation YJ_StatusCell

/**
 *  构造方法(在初始化对象的时候会调用)
 *  一般在这个方法中添加需要显示的子控件
 */
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        // 1.头像
        UIImageView *iconView = [[UIImageView alloc] init];
        [self.contentView addSubview:iconView];
        self.iconPicView = iconView;
        
        // 2.昵称
        UILabel *nameView = [[UILabel alloc] init];
        nameView.font = YJ_NameFont;
        nameView.alpha = 0.5;
        [self.contentView addSubview:nameView];
        self.usernameView = nameView;
        
        // 3.时间
        UILabel *timerView = [[UILabel alloc] init];
        timerView.font = YJ_NameFont;
        timerView.alpha = 0.5;
        [self.contentView addSubview:timerView];
        self.timerView = timerView;
        
        // 4.正文
        UILabel *textView = [[UILabel alloc] init];
        textView.numberOfLines = 0;
        textView.font = YJ_TextFont;
        textView.alpha = 0.7;
        [self.contentView addSubview:textView];
        self.textContentView = textView;
        
        // 5.配图
        UIImageView *pictureView = [[UIImageView alloc] init];
        [self.contentView addSubview:pictureView];
        self.pictureView = pictureView;
        
        // 评论点赞栏
        UIView *commentView = [[UIView alloc]init];
        commentView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
        [self.contentView addSubview:commentView];
        self.commentView = commentView;
        
        // 分隔栏
        UIView *cellPaddingView = [[UIView alloc]init];
        cellPaddingView.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1];
        [self.contentView addSubview:cellPaddingView];
        self.cellPaddingView = cellPaddingView;
    }
    return self;
}

/**
 *  在这个方法中设置子控件的frame和显示数据
 */
- (void)setStatusFrame:(YJ_StatusFrame *)statusFrame
{
    _statusFrame = statusFrame;
    
    // 1.设置数据
    [self settingData];
    
    // 2.设置frame
    [self settingFrame];
}

/**
 *  设置数据
 */
- (void)settingData
{
    // 微博数据
    YJ_Status *status = self.statusFrame.status;
    
    // 1.头像
    self.iconPicView.image = [UIImage imageNamed:status.iconPic];
    
    // 2.昵称
    self.usernameView.text = status.username;
    
    // 3.时间
   self.timerView.text = status.timer;
    
    // 4.正文
    self.textContentView.text = status.textContent;
    
    // 5.配图
    if (status.picture) { // 有配图
        self.pictureView.hidden = NO;
        self.pictureView.image = [UIImage imageNamed:status.picture];
    } else { // 没有配图
        self.pictureView.hidden = YES;
    }
}

/**
 *  计算文字尺寸
 *
 *  @param text    需要计算尺寸的文字
 *  @param font    文字的字体
 *  @param maxSize 文字的最大尺寸
 */
- (CGSize)sizeWithText:(NSString *)text font:(UIFont *)font maxSize:(CGSize)maxSize
{
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

/**
 *  设置frame
 */
- (void)settingFrame
{
    // 1.头像
    self.iconPicView.frame = self.statusFrame.incoPicF;
    
    // 2.昵称
    self.usernameView.frame = self.statusFrame.usernameF;
    
    // 3.时间
    self.timerView.frame = self.statusFrame.timerF;
    
    // 4.正文
    self.textContentView.frame = self.statusFrame.textContentF;
    
    // 5.配图
    if (self.statusFrame.status.picture) {// 有配图
        self.pictureView.frame = self.statusFrame.pictureF;
    }
    
    self.cellPaddingView.frame = self.statusFrame.cellPaddingF;
    
    self.commentView.frame = self.statusFrame.commentF;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"status";
    YJ_StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[YJ_StatusCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}


@end
